home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Halma 1.1.source Folder / Halma ƒ / Halma code ƒ / halma main window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-25  |  11.0 KB  |  505 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        halma main window.c
  4.  
  5. Purpose:    This module handles initializing/drawing/dealing with the
  6.             main window.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "halma main window.h"
  26. #include "halma meat.h"
  27. #include "halma snow.h"
  28. #include "halma endgame.h"
  29. #include "buttons.h"
  30. #include "environment.h"
  31. #include "graphics.h"
  32. #include "util.h"
  33. #include "program globals.h"
  34. #include "sounds.h"
  35.  
  36. /* internal procedures for halma main window.c only */
  37. void SetupTheMainWindow(WindowDataHandle theData);
  38. void ShutDownTheMainWindow(WindowDataHandle theData);
  39. void InitializeTheMainWindow(WindowDataHandle theData);
  40. void OpenTheMainWindow(WindowDataHandle theData);
  41. void CloseTheMainWindow(WindowDataHandle theData);
  42. void IdleInMainWindow(WindowDataHandle theData);
  43. void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar);
  44. void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint);
  45. void DisposeTheMainWindow(WindowDataHandle theData);
  46. void ActivateTheMainWindow(WindowDataHandle theData);
  47. void DeactivateTheMainWindow(WindowDataHandle theData);
  48. void DrawTheMainWindow(WindowDataHandle theData, short theDepth);
  49.  
  50. static    short            gOldForegroundTime;        /* stored foreground wait time */
  51. static    short            gDisplayedScore;
  52.  
  53. CIconHandle        gPieceIconColor;
  54. Handle            gPieceIconBW;
  55. Rect            gButtonRect[NUM_BUTTONS];
  56.  
  57. short MainWindowDispatch(WindowDataHandle theData, short theMessage,
  58.     unsigned long misc)
  59. {
  60.     short            theDepth;
  61.     unsigned char    theChar;
  62.     Point            thePoint;
  63.     GrafPtr            curPort;
  64.     
  65.     switch (theMessage)
  66.     {
  67.         case kNull:
  68.             IdleInMainWindow(theData);
  69.             return kSuccess;
  70.             break;
  71.         case kUpdate:
  72.             theDepth=misc&0x7fff;
  73.             DrawTheMainWindow(theData, theDepth);
  74.             return kSuccess;
  75.             break;
  76.         case kInitialize:
  77.             InitializeTheMainWindow(theData);
  78.             return kFailure;
  79.         case kOpen:
  80.             OpenTheMainWindow(theData);
  81.             return kSuccess;
  82.             break;
  83.         case kClose:
  84.             CloseTheMainWindow(theData);
  85.             return kSuccess;
  86.             break;
  87.         case kKeydown:
  88.             theChar=misc&charCodeMask;
  89.             KeyPressedInMainWindow(theData, theChar);
  90.             return kSuccess;
  91.             break;
  92.         case kMousedown:
  93.              thePoint.h=(misc>>16)&0x7fff;
  94.              thePoint.v=misc&0x7fff;
  95.              MouseClickedInMainWindow(theData, thePoint);
  96.              return kSuccess;
  97.              break;
  98.          case kActivate:
  99.              ActivateTheMainWindow(theData);
  100.              return kSuccess;
  101.              break;
  102.          case kDeactivate:
  103.              DeactivateTheMainWindow(theData);
  104.              return kSuccess;
  105.              break;
  106.         case kStartup:
  107.             SetupTheMainWindow(theData);
  108.             return kSuccess;
  109.             break;
  110.         case kDispose:
  111.             DisposeTheMainWindow(theData);
  112.             return kSuccess;
  113.             break;
  114.         case kShutdown:
  115.             ShutDownTheMainWindow(theData);
  116.             return kSuccess;
  117.             break;
  118.         case kUndo:
  119.             if (!ShowingEndGameQQ())
  120.             {
  121.                 if (gThisJumpString[0]>0x00)
  122.                     UndoOneJump(theData);
  123.                 else
  124.                     UndoWholeMove(theData);
  125.             }
  126.             return kSuccess;
  127.             break;
  128.         case kChangeDepth:
  129.             gDisplayedScore=-1;
  130.             ShutDownTheSnow();
  131.             InitTheSnow();
  132.             GetPort(&curPort);
  133.             FillRect(&(curPort->portRect), black);
  134.             break;
  135.     }
  136.     
  137.     return kFailure;
  138. }
  139.  
  140. void SetupTheMainWindow(WindowDataHandle theData)
  141. {
  142.     unsigned char    *titleStr="\pHalma";
  143.     short            row, col;
  144.     
  145.     (**theData).maxDepth=8;
  146.     (**theData).windowType=noGrowDocProc;
  147.     (**theData).hasCloseBox=TRUE;
  148.     Mymemcpy((Ptr)((**theData).windowTitle), (Ptr)titleStr, titleStr[0]+1);
  149.     
  150.     if (gHasColorQD)
  151.         gPieceIconColor=GetCIcon(128);
  152.     gPieceIconBW=GetIcon(128);
  153.     
  154.     for (row=0; row<9; row++)
  155.     {
  156.         for (col=0; col<9; col++)
  157.         {
  158.             SetRect(&gButtonRect[row*9+col], 10+col*30, 10+row*30, 40+col*30, 40+row*30);
  159.         }
  160.     }
  161. }
  162.  
  163. void ShutDownTheMainWindow(WindowDataHandle theData)
  164. {
  165.     if (gHasColorQD)
  166.         DisposeCIcon(gPieceIconColor);
  167.     ReleaseResource(gPieceIconBW);
  168. }
  169.  
  170. void InitializeTheMainWindow(WindowDataHandle theData)
  171. {
  172.     (**theData).windowWidth=155+gNumColumns*30;
  173.     (**theData).windowHeight=20+gNumRows*30;
  174.     gDisplayedScore=-1;
  175.     GetDateTime((unsigned long*)&randSeed);
  176.     InitTheSnow();
  177.     InitTheEndGame();
  178.     DoSound(sound_startgame, TRUE);
  179. }
  180.  
  181. void OpenTheMainWindow(WindowDataHandle theData)
  182. {
  183.     GrafPtr            curPort;
  184.     
  185.     (**theData).offscreenNeedsUpdate=TRUE;
  186.     if (SetPortToOffscreen(theData))
  187.     {
  188.         GetPort(&curPort);
  189.         FillRect(&(curPort->portRect), black);
  190.         RestorePortToScreen(theData);
  191.     }
  192. }
  193.  
  194. void CloseTheMainWindow(WindowDataHandle theData)
  195. {
  196.     ShutDownTheSnow();
  197. }
  198.  
  199. void IdleInMainWindow(WindowDataHandle theData)
  200. {
  201.     SnowIdle(theData);
  202. }
  203.  
  204. void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar)
  205. {
  206.     short            theRow, theColumn;
  207.     
  208.     if (ShowingEndGameQQ())
  209.     {
  210.         DontShowEndGame(theData);
  211.         return;
  212.     }
  213.     
  214.     if (GameOverQQ())
  215.         return;
  216.     
  217.     if ((gStickyButtonRow==-1) && (gStickyButtonColumn==-1))
  218.         return;
  219.         
  220.     theRow=theColumn=-1;
  221.     if ((theChar>='A') && (theChar<='Z'))
  222.         theChar|=0x20;
  223.     
  224.     switch (theChar)
  225.     {
  226.         case 'u':
  227.         case '7':
  228.         case 'i':
  229.         case '8':
  230.         case 'o':
  231.         case '9':
  232.             theRow=gStickyButtonRow-2;
  233.             break;
  234.         case 'j':
  235.         case '4':
  236.         case 'l':
  237.         case '6':
  238.         case 'a':
  239.         case 'd':
  240.             theRow=gStickyButtonRow;
  241.             break;
  242.         case 'm':
  243.         case ',':
  244.         case '.':
  245.         case '1':
  246.         case '2':
  247.         case '3':
  248.             theRow=gStickyButtonRow+2;
  249.             break;
  250.         case 'q':
  251.         case 'w':
  252.         case 'e':
  253.             theRow=gStickyButtonRow-1;
  254.             break;
  255.         case 'z':
  256.         case 'x':
  257.         case 'c':
  258.             theRow=gStickyButtonRow+1;
  259.             break;
  260.     }
  261.     switch (theChar)
  262.     {
  263.         case 'u':
  264.         case 'j':
  265.         case 'm':
  266.         case '7':
  267.         case '4':
  268.         case '1':
  269.             theColumn=gStickyButtonColumn-2;
  270.             break;
  271.         case 'i':
  272.         case ',':
  273.         case '8':
  274.         case '2':
  275.         case 'w':
  276.         case 'x':
  277.             theColumn=gStickyButtonColumn;
  278.             break;
  279.         case 'o':
  280.         case 'l':
  281.         case '.':
  282.         case '9':
  283.         case '6':
  284.         case '3':
  285.             theColumn=gStickyButtonColumn+2;
  286.             break;
  287.         case 'q':
  288.         case 'a':
  289.         case 'z':
  290.             theColumn=gStickyButtonColumn-1;
  291.             break;
  292.         case 'e':
  293.         case 'd':
  294.         case 'c':
  295.             theColumn=gStickyButtonColumn+1;
  296.             break;
  297.     }
  298.     if ((theRow<0) || (theRow>=gNumRows) || (theColumn<0) || (theColumn>=gNumColumns))
  299.         return;
  300.     
  301.     switch (GetMoveType(theRow, theColumn))
  302.     {
  303.         case kValidJump:
  304.             DoValidJump(theData, theRow, theColumn);
  305.             break;
  306.         case kValidFirstMove:
  307.             DoValidFirstMove(theData, theRow, theColumn, TRUE);
  308.             break;
  309.         case kValidSingleMove:
  310.             DoValidSingleMove(theData, theRow, theColumn);
  311.             break;
  312.         case kValidSingleMoveNewMove:
  313.             DoValidSingleMoveNewMove(theData, theRow, theColumn);
  314.             break;
  315.         case kOffBoard:
  316.             DoOffBoard(theData);
  317.             break;
  318.         case kCantJumpThere:
  319.             DoCantJumpThere(theData);
  320.             break;
  321.         case kMustJumpOverPiece:
  322.             DoMustJumpOverPiece(theData);
  323.             break;
  324.         case kCantSelectNonPiece:
  325.             DoCantSelectNonPiece(theData);
  326.             break;
  327.         case kSpaceNotEmpty:
  328.             DoSpaceNotEmpty(theData, theRow, theColumn);
  329.             break;
  330.         case kAlreadyHighlighted:
  331.             DoAlreadyHighlighted(theData, theRow, theColumn);
  332.             break;
  333.     }
  334.     CheckEndGame(theData, theRow, theColumn);
  335. }
  336.  
  337. void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint)
  338. {
  339.     short            theRow, theColumn;
  340.     
  341.     if (ShowingEndGameQQ())
  342.     {
  343.         DontShowEndGame(theData);
  344.         return;
  345.     }
  346.     
  347.     if (GameOverQQ())
  348.         return;
  349.     
  350.     switch (GetMoveType(theRow=(thePoint.v-10)/30, theColumn=(thePoint.h-10)/30))
  351.     {
  352.         case kValidJump:
  353.             DoValidJump(theData, theRow, theColumn);
  354.             break;
  355.         case kValidFirstMove:
  356.             DoValidFirstMove(theData, theRow, theColumn, TRUE);
  357.             break;
  358.         case kValidSingleMove:
  359.             DoValidSingleMove(theData, theRow, theColumn);
  360.             break;
  361.         case kValidSingleMoveNewMove:
  362.             DoValidSingleMoveNewMove(theData, theRow, theColumn);
  363.             break;
  364.         case kOffBoard:
  365.             DoOffBoard(theData);
  366.             break;
  367.         case kCantJumpThere:
  368.             DoCantJumpThere(theData);
  369.             break;
  370.         case kMustJumpOverPiece:
  371.             DoMustJumpOverPiece(theData);
  372.             break;
  373.         case kCantSelectNonPiece:
  374.             DoCantSelectNonPiece(theData);
  375.             break;
  376.         case kSpaceNotEmpty:
  377.             DoSpaceNotEmpty(theData, theRow, theColumn);
  378.             break;
  379.         case kAlreadyHighlighted:
  380.             DoAlreadyHighlighted(theData, theRow, theColumn);
  381.             break;
  382.     }
  383.     CheckEndGame(theData, theRow, theColumn);
  384. }
  385.  
  386. void DisposeTheMainWindow(WindowDataHandle theData)
  387. {
  388. }
  389.  
  390. void ActivateTheMainWindow(WindowDataHandle theData)
  391. {
  392.     gOldForegroundTime=gForegroundWaitTime;
  393.     gForegroundWaitTime=0;
  394. }
  395.  
  396. void DeactivateTheMainWindow(WindowDataHandle theData)
  397. {
  398.     gForegroundWaitTime=gOldForegroundTime;
  399. }
  400.  
  401. void DrawTheMainWindow(WindowDataHandle theData, short theDepth)
  402. {
  403.     RGBColor        oldForeColor, oldBackColor;
  404.     GrafPtr            curPort;
  405.     short            i,j;
  406.     Str31            theStr;
  407.     Rect            tempRect;
  408.     
  409.     if (theDepth>2)
  410.     {
  411.         GetForeColor(&oldForeColor);
  412.         GetBackColor(&oldBackColor);
  413.     }
  414.     
  415.     GetPort(&curPort);
  416.     tempRect=curPort->portRect;
  417.     tempRect.bottom=10;
  418.     FillRect(&tempRect, black);
  419.     tempRect=curPort->portRect;
  420.     tempRect.right=gNumColumns*30+20;
  421.     FillRect(&tempRect, black);
  422.     tempRect=curPort->portRect;
  423.     tempRect.top=gNumRows*30-25;
  424.     FillRect(&tempRect, black);
  425.     MoveTo(gNumColumns*30+20, gNumRows*30-25);
  426.     ForeColor(whiteColor);
  427.     Line(125, 0);
  428.     ForeColor(blackColor);
  429.     TextFont(geneva);
  430.     TextSize(9);
  431.     TextMode(srcXor);
  432.     MoveTo(gNumColumns*30+20, gNumRows*30-9);
  433.     DrawString("\pSolution so far:");
  434.     MoveTo(gNumColumns*30+20, gNumRows*30+6);
  435.     DrawString("\pMinimal solution:");
  436.     MoveTo(gNumColumns*30+101, gNumRows*30+6);
  437.     i=BestSolution();
  438.     if (i>0)
  439.     {
  440.         NumToString(BestSolution(), theStr);
  441.         DrawString(theStr);
  442.         DrawString("\p moves");
  443.     }
  444.     else
  445.         DrawString("\punknown");
  446.     
  447.     gDisplayedScore=-1;
  448.     DrawScoreAndStuff(theData, theDepth);
  449.     
  450.     if (ShowingEndGameQQ())
  451.         EndTheGame(theData, theDepth);
  452.     else
  453.     {
  454.         for (i=0; i<gNumRows; i++)
  455.         {
  456.             for (j=0; j<gNumColumns; j++)
  457.             {
  458.                 switch (Board[i][j])
  459.                 {
  460.                     case kPiece:
  461.                         Draw3DButton(&gButtonRect[i*9+j], 0L, (**theData).windowDepth>2 ?
  462.                             gPieceIconColor : gPieceIconBW, theDepth, FALSE);
  463.                         break;
  464.                     case kHighlightedPiece:
  465.                         Draw3DButton(&gButtonRect[i*9+j], 0L, (**theData).windowDepth>2 ?
  466.                             gPieceIconColor : gPieceIconBW, theDepth, TRUE);
  467.                         break;
  468.                     default:
  469.                         Draw3DButton(&gButtonRect[i*9+j], 0L, 0L, theDepth, FALSE);
  470.                         break;
  471.                 }
  472.             }
  473.         }
  474.     }
  475.     
  476.     DrawScoreAndStuff(theData, theDepth);
  477.     
  478.     if (theDepth>2)
  479.     {
  480.         RGBForeColor(&oldForeColor);
  481.         RGBBackColor(&oldBackColor);
  482.     }
  483. }
  484.  
  485. void DrawScoreAndStuff(WindowDataHandle theData, short theDepth)
  486. {
  487.     Rect            tempRect;
  488.     GrafPtr            curPort;
  489.     Str255            ts;
  490.     
  491.     if (gNumMoves==gDisplayedScore)
  492.         return;
  493.     
  494.     GetPort(&curPort);
  495.     SetRect(&tempRect, gNumColumns*30+101, gNumRows*30-20, gNumColumns*30+125,
  496.         gNumRows*30-9);
  497.     MoveTo(gNumColumns*30+101, gNumRows*30-9);
  498.     NumToString(gNumMoves, ts);
  499.     FillRect(&tempRect, black);
  500.     TextMode(srcXor);
  501.     DrawString(ts);
  502.     TextMode(srcCopy);
  503.     gDisplayedScore=gNumMoves;
  504. }
  505.